package com.rc.saas.tenant.job;

import com.rc.saas.tenant.common.constant.CommonConstant;
import com.rc.saas.tenant.model.operator.TenantOperator;
import com.rc.saas.tenant.model.operator.TenantOperatorExample;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenService;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenServiceExample;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantExample;
import com.rc.saas.tenant.model.tenant.TenantOpenServiceExample;
import com.rc.saas.tenant.service.operator.TenantOperatorOpenServiceService;
import com.rc.saas.tenant.service.operator.TenantOperatorService;
import com.rc.saas.tenant.service.tenant.TenantOpenServiceService;
import com.rc.saas.tenant.service.tenant.TenantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class TenantOperatorOpenServiceTask {
    Logger logger = LoggerFactory.getLogger(TenantOperatorOpenServiceTask.class);

    private static final String TENANT_CODE = "914401153";

    @Autowired
    private TenantOperatorOpenServiceService tenantOperatorOpenServiceService;

    @Autowired
    private TenantOperatorService tenantOperatorService;

    @Autowired
    private TenantService tenantService;

    public void run() {
        try {
            TenantExample tenantExample = new TenantExample();
            tenantExample.createCriteria().andStatusEqualTo("0");
            List<Tenant> tenants = tenantService.selectByExample(tenantExample);
            for (int i = 0; i < tenants.size(); i++) {
                String tenantCode=tenants.get(i).getTenantCode();
                //todo 瑞晨分销商开通分销商结算服务设置
                TenantOperatorOpenServiceExample tenantOperatorOpenServiceExample = new TenantOperatorOpenServiceExample();
                tenantOperatorOpenServiceExample.createCriteria().andTenantCodeEqualTo(tenantCode).andServiceTypeCodeEqualTo(CommonConstant.SERVICE_TYPE_CODE);
                //开通了分销商结算服务的分销商编码
                List<String> tenantOperatorCodes = tenantOperatorOpenServiceService.selectByExample(tenantOperatorOpenServiceExample).stream().map(tenantOpenService -> tenantOpenService.getTenantOperatorCode()).distinct().collect(Collectors.toList());
                if (tenantOperatorCodes.size()>0 && !CollectionUtils.isEmpty(tenantOperatorCodes)){
                    TenantOperatorExample example = new TenantOperatorExample();
                    //查出未开通分销商结算服务的分销商信息
                    example.createCriteria().andTenantCodeEqualTo(tenantCode).andTenantOperatorCodeNotIn(tenantOperatorCodes);
                    List<String> tenantOperatorCodes1 = tenantOperatorService.selectByExample(example).stream().map(tenantOperator -> tenantOperator.getTenantOperatorCode()).distinct().collect(Collectors.toList());
                    for (int j = 0; j < tenantOperatorCodes1.size(); j++) {
                        TenantOperatorOpenService tenantOperatorOpenService = new TenantOperatorOpenService();
                        tenantOperatorOpenService.setTenantCode(tenantCode);
                        tenantOperatorOpenService.setTenantOperatorCode(tenantOperatorCodes1.get(j));
                        tenantOperatorOpenService.setServiceTypeCode(CommonConstant.SERVICE_TYPE_CODE);
                        tenantOperatorOpenService.setStatus("0");
                        tenantOperatorOpenService.setCreatedTime(new Date());
                        tenantOperatorOpenServiceService.insertSelective(tenantOperatorOpenService);
                    }
                    logger.info("自动设置分销商结算服务成功[自动设置分销商结算服务成功]");
                }
            }
        } catch (Exception e) {
            logger.error("自动设置分销商结算服务失败[自动设置分销商结算服务失败]", e);
        }

    }
}
